package com.qige.student_system;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static final Scanner INPUT = StudentMenu.INPUT;
    ArrayList<Student> list = StudentMenu.list;
    public static void menu(ArrayList<Student> list){
        outer:while (true){
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int choice = INPUT.nextInt();
            switch (choice){
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    updateStudent(list);
                    break;
                case 4:
                    check(list);
                    break;
                    default:
                        System.out.println("退出系统");
                        break outer;
            }
        }
    }
    public static void addStudent(ArrayList<Student> list){
        String sid;
        while (true){
            System.out.println("请输入学生学号:");
            sid = INPUT.next();
            int index = getIndex(list,sid);
            if (index == -1){
                break;
            }else {
                System.out.println("学号重复,请重新输入:");
            }
        }
        System.out.println("请输入学生姓名:");
        String name = INPUT.next();
        System.out.println("请输入学生年龄:");
        int age = INPUT.nextInt();
        System.out.println("请输入学生居住地:");
        String address = INPUT.next();
        Student stu = new Student(sid,name,age,address);
        list.add(stu);
        System.out.println("添加成功！");
    }
    public static void deleteStudent(ArrayList<Student> list){
        String sid;
        while (true){
            System.out.println("请输入学生学号:");
            sid = INPUT.next();
            int index = getIndex(list,sid);
            if (index == -1){
                System.out.println("该学生信息不存在,请重新输入！");
            }else {
                list.remove(index);
                System.out.println("删除成功！");
                break;
            }
        }
    }
    public static void updateStudent(ArrayList<Student> list){
        String sid;
        while (true){
            System.out.println("请输入你要修改的学生学号:");
            sid = INPUT.next();
            int index = getIndex(list,sid);
            if (index == -1){
                System.out.println("该学生信息不存在,请重新输入！");
            }else {
                System.out.println("请输入学生的新姓名:");
                String name = INPUT.next();
                System.out.println("请输入学生的新年龄:");
                int age = INPUT.nextInt();
                System.out.println("请输入学生的新居住地:");
                String address = INPUT.next();
                list.get(index).setName(name);
                list.get(index).setAge(age);
                list.get(index).setAddress(address);
                System.out.println("修改成功！");
                break;
            }
        }
    }
    public static void check(ArrayList<Student> list){
        if (list.size() == 0 || list == null){
            System.out.println("暂无学生信息,请添加信息再查询！");
            return;
        }else {
            System.out.println("学号\t姓名\t年龄\t生日");
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getSid() + " " + stu.getName() + " " + stu.getAge() + " " + stu.getAddress());
            }
        }
    }
    public static int getIndex(ArrayList<Student> list,String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getSid().equals(sid)){
                index = i;
            }
        }
        return index;
    }
}
